{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e616503b",
   "metadata": {},
   "source": [
    "# General operations for the client and cluster\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Client Options"
   ],
   "id": "e077c7efcb0cb152"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "pip install aistore"
   ],
   "id": "88fee10ec2d02c0e"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from aistore import Client\n",
    "\n",
    "# Use the client class to access the AIS cluster:\n",
    "ais_url = \"http://localhost:8080\"\n",
    "client = Client(ais_url)"
   ],
   "id": "15587ae06f7bc787"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### The client provides access to all the sdk functionality"
   ],
   "id": "1e0e13c26270736b"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# All operations on buckets and objects inside\n",
    "client.bucket(\"bucket-name\")\n",
    "# For interacting with the AIS cluster\n",
    "client.cluster()\n",
    "# For starting, waiting on, and querying async jobs\n",
    "client.job()\n",
    "# For creating ETL jobs\n",
    "client.etl(\"etl_name\")"
   ],
   "id": "7b7f06ae962224ee"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Cluster operations"
   ],
   "id": "7a5b30173114e31d"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Check if aistore is running"
   ],
   "id": "1a8fbf61fcbd894"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client.cluster().is_ready()"
   ],
   "id": "5048fbd3f84e401"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [],
   "id": "e3f81da1295223d1"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### View cluster info"
   ],
   "id": "5292e59d4bfa8023"
  },
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "is_executing": true
    }
   },
   "source": [
    "client.cluster().get_info().dict()"
   ],
   "id": "3975e685fd2cd2f9",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### List all buckets in the cluster"
   ],
   "id": "177c7095f02d8049"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client.cluster().list_buckets()"
   ],
   "id": "5cca024ea2dd7046"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Working with jobs\n",
    "\n",
    "Some intensive actions run asynchronously from the sdk calls and will instead return a job id. This job id can be used to check the status and wait on the job if needed."
   ],
   "id": "9e9a9e9269c6cda2"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "first_bck = client.bucket(\"my-first-bck\")\n",
    "first_bck.create()\n",
    "# Returns the id of the job\n",
    "rename_job_id = first_bck.rename(\"new-bck\")\n",
    "rename_job = client.job(rename_job_id)\n",
    "# Check status\n",
    "rename_job.status()\n",
    "# Wait until job is finished\n",
    "rename_job.wait()"
   ],
   "id": "3b5a3fe4864159f1"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Querying jobs\n",
    "You can also query the status of multiple jobs, filtered by the running cluster node or the kind of job"
   ],
   "id": "95facc37e704b2c"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# View all currently running jobs\n",
    "client.cluster().list_running_jobs()\n",
    "# View all jobs of a certain type\n",
    "# client.cluster().list_running_jobs(job_kind=\"specific job kind\")"
   ],
   "id": "a9c17f41ab69a356"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Querying ETLs\n",
    "\n",
    "You can query for a list of running ETLs"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "af6fcf459f921fe4"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "client.cluster().list_running_etls()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "13ef650b8e770056"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e666d6479b9f16fe"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1 (main, Dec  7 2022, 01:11:34) [GCC 11.3.0]"
  },
  "vscode": {
   "interpreter": {
    "hash": "ead1b95f633dc9c51826328e1846203f51a198c6fb5f2884a80417ba131d4e82"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
